package com.encryption.controls.util
{
	import com.encryption.controls.AdvancedComboBoxFormItem;
	import com.encryption.controls.AdvancedForm;
	import com.encryption.controls.AdvancedFormItem;
	
	import mx.containers.Form;
	import mx.containers.FormItem;
	import mx.core.UIComponent;
	import mx.utils.StringUtil;

	public class InputValidateUtil
	{
		public function InputValidateUtil()
		{
		}
		
		public static function validateEmpty(form:AdvancedForm):Boolean
		{
			var hasEmpty:Boolean = false;
			for each(var component:UIComponent in form.getChildren()){
				if(component is AdvancedFormItem){
					var formItem:AdvancedFormItem = component as AdvancedFormItem;
					var name:String = formItem.label;
					var inputText:String = formItem.inputText;
					if(inputText == null || StringUtil.trim(inputText) == ""){
						hasEmpty = true;
						formItem.errorToolTip = "请填写" + name.substring(0, name.length - 1);
					}else{
						formItem.errorToolTip = "";
					}
				}
			}
			
			return hasEmpty;
		}
		
		public static function validatePassword(psdFormItem:AdvancedFormItem, 
												ensurePsdFormItem:AdvancedFormItem):Boolean
		{
			var equal:Boolean = psdFormItem.trimInputText == ensurePsdFormItem.trimInputText;
			if(equal)
				psdFormItem.errorToolTip = "两次输入的密码不一致";
			
			return equal;
		}
		
		public static function clearFormItem(form:Form):void
		{
			for each(var component:UIComponent in form.getChildren()){
				if(component is AdvancedFormItem){
					var formItem:AdvancedFormItem = component as AdvancedFormItem;
					formItem.inputText = "";
					formItem.errorToolTip = "";
				}else if(component is AdvancedComboBoxFormItem){
					var item:AdvancedComboBoxFormItem = component as AdvancedComboBoxFormItem;
					item.inputIndex = 0;
					item.errorToolTip = "";
				}
			}
		}
	}
}